/* 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

goog.provide('kramerius.audio.ui.PlayPauseButton');

goog.require('kramerius.audio.ui.Button');
goog.require('kramerius.audio.debug_utils');
//goog.require('kramerius.audio.ControllerMock');
goog.require('goog.ui.Component');
goog.require('goog.ui.Control');


/** @const */ 
//var PLAY_ENABLED = 'btnPlayEnabled';
/** @const */ 
//var PLAY_DISABLED = 'btnPlayDisabled';
/** @const */ 
//var PAUSE_ENABLED = 'btnPauseEnabled';

/** @const */ 
var PLAY = 'btnPlay';
/** @const */ 
//var PLAY_HOVER = 'btnPlayHover';
/** @const */ 
var PAUSE = 'btnPause';
/** @const */ 
//var PAUSE_HOVER = 'btnPauseHover';

/** @const */ 
var PLAY_PAUSE_BUTTON_STATE_PLAY_ENABLED = 'PLAY_ENABLED';
/** @const */ 
var PLAY_PAUSE_BUTTON_STATE_PLAY_DISABLED = 'PLAY_DISABLED';
/** @const */ 
var PLAY_PAUSE_BUTTON_STATE_PAUSE_ENABLED = 'PAUSE_ENABLED';


/**
 * @constructor 
 * @param {Element} container Element containing this button.
 * @param {kramerius.audio.Controller} controller Controller of the audio player.
 * @param {boolean} loggingEnabled whether logging should be enabled (for debugging)
 */
kramerius.audio.ui.PlayPauseButton = function(container, controller, loggingEnabled){
    kramerius.audio.ui.Button.call(this, container, controller);
    /** @private */
    this.controller_.setPlayPauseButton(this);
    /** @private */
    this.btnControl_ = this.initializeControl_();
    /** @private */
    this.loggingEnabled_ = loggingEnabled;
    /** @private */
    //this.setStatePlayDisabled();
    this.setStatePlayEnabled();
};
goog.inherits(kramerius.audio.ui.PlayPauseButton, kramerius.audio.ui.Button);


/** @private */
kramerius.audio.ui.PlayPauseButton.prototype.initializeControl_ = function(){
    var control = new goog.ui.Control();
    control.setDispatchTransitionEvents(goog.ui.Component.State.ACTION, true);
    var playerController = this.controller_;
    var button = this;
//    goog.events.listen(control,goog.ui.Component.EventType.ENTER,
//        function (e) {
//            button.mouseOver_ = true;
//            if (button.getState() == PLAY_PAUSE_BUTTON_STATE_PAUSE_ENABLED){
//                button.setContainerClass_(PAUSE);
//            }else{
//                button.setContainerClass_(PLAY);
//            }
//        });
//    goog.events.listen(control,goog.ui.Component.EventType.LEAVE,
//        function (e) {
//            button.mouseOver_ = false;
//            if (button.getState() == PLAY_PAUSE_BUTTON_STATE_PAUSE_ENABLED){
//                button.setContainerClass_(PAUSE);
//            }else{
//                button.setContainerClass_(PLAY);
//            }
//        });
//    
    goog.events.listen(control, goog.ui.Component.EventType.ACTION,
      function (e) {
            //button.log_('ACTION, state: ' + button.getState());
            var state = button.getState();
            if ( state == PLAY_PAUSE_BUTTON_STATE_PLAY_ENABLED){
                button.log_("playing/resuming");
                playerController.play();
                //playerController.resume();
            }else if ( state == PLAY_PAUSE_BUTTON_STATE_PLAY_DISABLED){
                button.log_("WARNING: clicked when inactive");
            }else if ( state == PLAY_PAUSE_BUTTON_STATE_PAUSE_ENABLED){
                button.log_("pausing");
                playerController.pause();
            }
        });
    //  kramerius.audio.utils.appendDebugListeners(button, "play button");
    control.decorate(this.container_);
    return control;
};


kramerius.audio.ui.PlayPauseButton.prototype.setStatePlayEnabled = function(){
    this.setState_(PLAY_PAUSE_BUTTON_STATE_PLAY_ENABLED);
    this.setContainerClass_(PLAY);
    
//    if (!!this.mouseOver_){
//        this.setContainerClass_(PLAY_HOVER);    
//    }else{
//        this.setContainerClass_(PLAY);
//    }
    //this.setContainerClass_(PLAY_ENABLED);
    new goog.ui.Tooltip(this.container_, "spustit přehrávání");
};


kramerius.audio.ui.PlayPauseButton.prototype.setStatePlayDisabled = function(){
    this.setState_(PLAY_PAUSE_BUTTON_STATE_PLAY_DISABLED);
//    if (!!this.mouseOver_){
//        this.setContainerClass_(PLAY_HOVER);    
//    }else{
//        this.setContainerClass_(PLAY);
//    }
    this.setContainerClass_(PLAY);
    //this.setContainerClass_(PLAY_DISABLED);
    new goog.ui.Tooltip(this.container_, "spustit přehrávání");
};


kramerius.audio.ui.PlayPauseButton.prototype.setStayPauseEnabled = function(){
    this.setState_(PLAY_PAUSE_BUTTON_STATE_PAUSE_ENABLED);
//    if (!!this.mouseOver_){
//        this.setContainerClass_(PAUSE_HOVER);    
//    }else{
//        this.setContainerClass_(PAUSE);
//    }
    this.setContainerClass_(PAUSE);
    //this.setContainerClass_(PAUSE_ENABLED);
    new goog.ui.Tooltip(this.container_, "pozastavit přehrávání");
};

/** @private */
kramerius.audio.ui.PlayPauseButton.prototype.setState_ = function(state){
    this.log_("setting state to " + state);
    this.state_ = state;
};

kramerius.audio.ui.PlayPauseButton.prototype.getState = function(){
    return this.state_;
};


/** @private */
kramerius.audio.ui.PlayPauseButton.prototype.log_= function(text){
    if(this.loggingEnabled_){
        console.log("PLAY/PAUSE button: " + text);
    }
}






    


